package net.sf.brutil.v0.light;

public class EscapeUtil {

	public static  String escapeSql(String sql){
		String retVal=sql;
		retVal=retVal.replace("'", "''");
		retVal=retVal.replace("\\", "\\\\");
		return retVal;
		
	}

	public static  String escapeAndQuoteSql(String sql){
		if (sql==null) return null;
		return "'"+escapeSql(sql)+"'";
	}

    public static String escapeProperty(String value){
    	//value=value.replace("'", "''"); ???
    	value=value.replace("\n", "\\n");
    	return value;
    }
    
    
    


	public static String escapeProperties(String value) {
		value=value.replace("\n", "\\n\\\n");
		return value;
	}
    
	 public static String escapeHtml(String what){
		 what=what.replaceAll("&","&amp;");
		 what=what.replaceAll("<", "&lt;");
	     what=what.replaceAll(">", "&gt;");
	     what=what.replaceAll("\n","<br/>");
            
	        return what;
	        
	    }
		/**
		 * spec from http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
		 * 
		 * it does not seem possible to escape characters above 0xff
		 */
		public static String escapeUrl(String agencyUrl) {
			
			String[]original={ "$"  ,"&"  ,","   ,"/"  ,":"   ,";"  ,"="  ,"?"  ,"@"  ," "  ,"<"   ,">"   ,"#","'","\""};
			
			agencyUrl=agencyUrl.replace("%","%25");
			
			for(int i=0;i<original.length;i++){
				int c=original[i].charAt(0);
				String s=Integer.toHexString(c);
				if (c<16)
					s="0"+s;
				agencyUrl=agencyUrl.replace(original[i],"%"+s);
			}
			
			
			
				
			
			return agencyUrl;
		}
}
